package com.it.controller;

import com.github.pagehelper.PageInfo;
import com.it.pojo.*;
import com.it.pojo.dto.InStoreDto;
import com.it.pojo.dto.OutStoreDto;
import com.it.pojo.dto.TimeDto;
import com.it.service.DriverService;
import com.it.service.InStoreService;
import com.it.service.OutStoreService;
import com.it.service.UserInfoService;
import com.it.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/27 9:34
 */
@Controller
@RequestMapping("/out")
public class OutStoreController {

    @Autowired
    private InStoreService inStoreService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private OutStoreService outStoreService;
    @Autowired
    private UserInfoService userInfoService;



    @GetMapping("/list")
    public ModelAndView list(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/pages/outstore/outstore");
        User user = (User) session.getAttribute("user");
        UserInfo info = userInfoService.getOneUserInfo(user.getUserId());
        //查看入库中未出库（InStatus=1）的信息
        List<InStore> instores = inStoreService.selectByInStatus(info.getCompanyId());
        modelAndView.addObject("instores", instores);

        List<Driver> driverList = driverService.findall();
        modelAndView.addObject("driverList", driverList);
        return modelAndView;
    }

    //通过inid获取入库信息
    @GetMapping("/list/{inid}")
    @ResponseBody
    public InStore inlist(@PathVariable("inid") String inId, HttpServletRequest request) {
        InStore instore = inStoreService.selectById(inId);
        request.setAttribute("instore", instore);
        return instore;
    }

    @PostMapping("/add")
    public String outlist(InStore instore, HttpSession session){
        User user = (User) session.getAttribute("user");
        outStoreService.addOut(instore,user);
        return "redirect:/out/list";
    }

    @GetMapping("/find")
    public ModelAndView find(OutStoreDto dto){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/outstore/list");
        PageInfo<OutStore> pageInfo = outStoreService.findPage(dto);
        modelAndView.addObject("pageInfo",pageInfo);
        List<UserInfo> users = userInfoService.findAll();
        modelAndView.addObject("users",users);
        List<TimeDto> dtoList = new ArrayList<>();
        dtoList.add(new TimeDto(1,"当天"));
        dtoList.add(new TimeDto(2,"本月"));
        dtoList.add(new TimeDto(3,"本季"));
        dtoList.add(new TimeDto(4,"本年"));
        modelAndView.addObject("timeList",dtoList);
        return modelAndView;
    }

    @PostMapping("/find")
    public ModelAndView one(OutStoreDto dto){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/outstore/list");
        PageInfo<OutStore> pageInfo = outStoreService.findPage(dto);
        modelAndView.addObject("pageInfo",pageInfo);
        List<UserInfo> users = userInfoService.findAll();
        modelAndView.addObject("users",users);
        List<TimeDto> dtoList = new ArrayList<>();
        dtoList.add(new TimeDto(1,"当天"));
        dtoList.add(new TimeDto(2,"本月"));
        dtoList.add(new TimeDto(3,"本季"));
        dtoList.add(new TimeDto(4,"本年"));
        modelAndView.addObject("timeList",dtoList);
        return modelAndView;
    }

    @PostMapping("/excel")
    public ModelAndView excel(OutStoreDto dto, HttpServletResponse response) throws Exception {
        System.out.println(dto);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/outstore/find");
        /*导入excel操作*/
        List<OutStore> excel = outStoreService.findExcel(dto);
        System.out.println(excel);
        ExcelUtils.export(response,"入库记录",excel,OutStore.class);
        return modelAndView;
    }

}
